package com.example.tellwin.mine.act;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tellwin.MainApplication;
import com.example.tellwin.R;
import com.example.tellwin.base.CpBaseActivty;
import com.example.tellwin.base.DaggerRxComponent;
import com.example.tellwin.mine.adapter.ShareRecordAdapter;
import com.example.tellwin.mine.bean.ShareRecordBean;
import com.example.tellwin.mine.contract.ShareRecordContract;
import com.example.tellwin.mine.presenter.ShareRecordPresenter;
import com.example.tellwin.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ShareRecordActivity extends CpBaseActivty implements ShareRecordContract.View {

    @BindView(R.id.share_record_rv)
    RecyclerView shareRecordRv;

    private List<ShareRecordBean> shareRecordBeans = new ArrayList<>();
    private ShareRecordAdapter shareRecordAdapter;

    @Inject
    ShareRecordPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_record);
        ButterKnife.bind(this);
        initViews();
        initEvents();
        initDatas();
    }

    @Override
    protected void initViews() {
        DaggerRxComponent.builder()
                .baseAppComponent(MainApplication.getAppComponent())
                .build()
                .inject(this);
        mPresenter.attachView(this);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        shareRecordRv.setLayoutManager(linearLayoutManager);
        shareRecordAdapter = new ShareRecordAdapter();
        shareRecordRv.setAdapter(shareRecordAdapter);
    }

    @Override
    protected void initEvents() {
        shareRecordRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (CommonUtils.isSlideToBottom(shareRecordRv)) {
                    mPresenter.loadMore();
                }
            }
        });
    }

    @Override
    protected void initDatas() {
        setTitle(R.string.share_record_);
        mPresenter.getShareRecord();
    }

    @Override
    public void shareRecordResult(List<ShareRecordBean> shareRecordBeans) {
        this.shareRecordBeans = shareRecordBeans;
        shareRecordAdapter.setShareRecordBeans(this.shareRecordBeans);
    }

    @Override
    public void loadMoreResult(List<ShareRecordBean> shareRecordBeans) {
        this.shareRecordBeans.addAll(shareRecordBeans);
        shareRecordAdapter.notifyDataSetChanged();
    }
}
